<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>键盘按下松开变色</title>
  <style>
    div {
      width: 500px;
      height: 500px;
      font-size: 24px;
      border: 1px solid #ccc;
      line-height: 500px;
      text-align: center;
    }
  </style>
</head>
<body>
  <div>按键盘试试</div>
  <script>
    var div = document.querySelector('div')
    document.onkeydown = function () {
      div.style.backgroundColor = 'pink'
    }
    document.onkeyup = function () {
      div.style.backgroundColor = 'purple'
    }
  </script>
</body>
</html>